//
//  HeaderViewSty2.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/9/26.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "HeaderViewSty2.h"
#import "Masonry.h"
#import "StuButton.h"
#import "UIColor+CL.h"
#import "BaseAllViewController.h"
@implementation HeaderViewSty2



-(instancetype)initWithDelegate:(id)delegate{
    self = [super init];
    if (self) {
        
        _delegate = delegate;
        //        [self createCollectionViewHeaderView];
    }
    
    return self;
    
}

-(void)initwithArr:(NSArray *)dataArr{
    self.dataArr = dataArr;
    [self createCollectionViewHeaderView];
}

-(UIView *)createCollectionViewHeaderView{
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changeImageView:) name:@"NeedChangeImageViewImage" object:nil];
    UIView *StuSuperView = self;
    
    UIView *baseView = [[UIView alloc]init];
    baseView.backgroundColor = [UIColor whiteColor];
    
    [StuSuperView addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(330/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
        make.top.equalTo(StuSuperView.mas_top);
    }];
    NSArray *iconNameArr = @[@"stu_my_order",@"stu_my_task",@"stu_my_journal",@"stu_my_attendance",@"stu_my_leave",@"stu_my_visit",@"stu_my_achievement",@"submenu_classification_normal"];
    NSLog(@"%@",_dataArr);
    if (self.dataArr.count <=7) {
        
        for (int i = 0; i<self.dataArr.count; i++) {
            //        for (int i = 0; i<6; i++) {
            StuButton *myBtn = [[StuButton alloc] init];
            myBtn.Btntag = i;
            [baseView addSubview:myBtn];
            NSMutableString *title = [NSMutableString stringWithFormat:@"%@",[[self.dataArr objectAtIndex:i] objectForKey:@"menuName"]];
            //            NSMutableString *strMenuName = [NSMutableString stringWithFormat:@"%@", [dic objectForKey:@"menuName"]];
            if ([title rangeOfString:@"手机端"].location != NSNotFound) {
                [title replaceCharactersInRange:[title rangeOfString:@"手机端"] withString:@""];
            }
            [myBtn setTitle:title forState:UIControlStateNormal];
            myBtn.titleLabel.font = [UIFont systemFontOfSize:28/2];
            [myBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"分类000%d",i+1]] forState:UIControlStateNormal];
            [myBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
            [myBtn addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(baseView.mas_left).offset((i%4)*KWIDTH/4);
                make.width.equalTo(@(KWIDTH/4));
                make.height.equalTo(@((124+36)/2));
                if (i<4) {
                    make.top.equalTo(StuSuperView.mas_top);
                }else{
                    make.top.equalTo(StuSuperView.mas_top).offset((124+36)/2);
                }
                
                //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
                
            }];
            
        }
        
    }else{
        for (int i = 0; i<8; i++) {
            StuButton *myBtn = [[StuButton alloc] init];
            myBtn.Btntag = i;
            [baseView addSubview:myBtn];
            NSMutableString *title = [NSMutableString stringWithFormat:@"%@",[[self.dataArr objectAtIndex:i] objectForKey:@"menuName"]];
            //            NSMutableString *strMenuName = [NSMutableString stringWithFormat:@"%@", [dic objectForKey:@"menuName"]];
            if ([title rangeOfString:@"手机端"].location != NSNotFound) {
                [title replaceCharactersInRange:[title rangeOfString:@"手机端"] withString:@""];
            }
            if (i == 7) {
                title = [NSMutableString stringWithFormat:@"%@",@"全部"];
            }
            [myBtn setTitle:title forState:UIControlStateNormal];
            myBtn.titleLabel.font = [UIFont systemFontOfSize:28/2];
            if (i == 7) {
                [myBtn setImage:[UIImage imageNamed:[iconNameArr objectAtIndex:i]] forState:UIControlStateNormal];
            }else{
                [myBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"分类000%d",i+1]] forState:UIControlStateNormal];
            }

            
            [myBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
            [myBtn addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(baseView.mas_left).offset((i%4)*KWIDTH/4);
                make.width.equalTo(@(KWIDTH/4));
                make.height.equalTo(@((124+36)/2));
                if (i<4) {
                    make.top.equalTo(StuSuperView.mas_top);
                }else{
                    make.top.equalTo(StuSuperView.mas_top).offset((124+36)/2);
                }
                
                //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
                
            }];
            
        }
    }
    
    
    
    //    StuButton *myBtn = [[StuButton alloc]init];
    //    myBtn.Btntag = 0;
    //    [baseView addSubview:myBtn];
    //
    //    StuButton *myBtn1 = [[StuButton alloc]init];
    //    myBtn1.Btntag = 1;
    //    [baseView addSubview:myBtn1];
    //
    //    StuButton *myBtn2 = [[StuButton alloc]init];
    //    myBtn2.Btntag = 2;
    //    [baseView addSubview:myBtn2];
    //
    //    StuButton *myBtn3 = [[StuButton alloc]init];
    //    myBtn3.Btntag = 3;
    //    [baseView addSubview:myBtn3];
    //    StuButton *myBtn4 = [[StuButton alloc]init];
    //    myBtn4.Btntag = 4;
    //    [baseView addSubview:myBtn4];
    //
    //    StuButton *myBtn5 = [[StuButton alloc]init];
    //    myBtn5.Btntag = 5;
    //    [baseView addSubview:myBtn5];
    //
    //    StuButton *myBtn6 = [[StuButton alloc]init];
    //    myBtn6.Btntag = 6;
    //    [baseView addSubview:myBtn6];
    //
    //    StuButton *myBtn7 = [[StuButton alloc]init];
    //    myBtn7.Btntag = 7;
    //    [baseView addSubview:myBtn7];
    //
    //
    //    NSArray *titArr = [NSArray arrayWithObjects:@"我的订单", @"我的作业", @"教学日志", @"考勤记录", @"请假记录", @"教学回访", @"成绩管理", @"全部", nil];
    //
    //    [myBtn setTitle:titArr[0] forState:UIControlStateNormal];
    //    [myBtn1 setTitle:titArr[1] forState:UIControlStateNormal];
    //    [myBtn2 setTitle:titArr[2] forState:UIControlStateNormal];
    //    [myBtn3 setTitle:titArr[3] forState:UIControlStateNormal];
    //    [myBtn4 setTitle:titArr[4] forState:UIControlStateNormal];
    //    [myBtn5 setTitle:titArr[5] forState:UIControlStateNormal];
    //    [myBtn6 setTitle:titArr[6] forState:UIControlStateNormal];
    //    [myBtn7 setTitle:titArr[7] forState:UIControlStateNormal];
    //
    //
    //
    //    myBtn.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn1.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn2.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn3.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn4.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn5.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn6.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    myBtn7.titleLabel.font = [UIFont systemFontOfSize:28/2];
    //    [myBtn addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn1 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn2 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn3 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn4 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn5 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn6 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [myBtn7 addTarget:self action:@selector(StuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    //
    //
    //
    //    [myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(baseView.mas_left).offset(0);
    //        make.width.equalTo(myBtn1.mas_width);
    //        make.height.equalTo(@((124+36)/2));
    //        make.top.equalTo(StuSuperView.mas_top);
    //        //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
    //
    //    }];
    //    [myBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn.mas_right);
    //        make.width.equalTo(myBtn.mas_width);
    //        make.height.equalTo(myBtn.mas_height);
    //        make.bottom.equalTo(myBtn.mas_bottom);
    //    }];
    //
    //    [myBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn1.mas_right);
    //        make.width.equalTo(myBtn1.mas_width);
    //        make.height.equalTo(myBtn1.mas_height);
    //        make.bottom.equalTo(myBtn1.mas_bottom);
    //        //            make.right.equalTo(myBtn3.mas_right).offset(-20);
    //    }];
    //
    //    [myBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn2.mas_right);
    //        make.width.equalTo(myBtn2.mas_width);
    //        make.height.equalTo(myBtn2.mas_height);
    //        make.bottom.equalTo(myBtn2.mas_bottom);
    //        make.right.equalTo(baseView.mas_right).offset(-0);
    //    }];
    //    [myBtn4 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(baseView.mas_left).offset(0);
    //        make.width.equalTo(myBtn1.mas_width);
    //        make.height.equalTo(@((124+36)/2));
    //        make.top.equalTo(myBtn.mas_bottom);
    //        //             make.bottom.equalTo(baseView.mas_bottom).offset(-20);
    //
    //    }];
    //    [myBtn5 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn4.mas_right);
    //        make.width.equalTo(myBtn4.mas_width);
    //        make.height.equalTo(myBtn4.mas_height);
    //        make.bottom.equalTo(myBtn4.mas_bottom);
    //    }];
    //    [myBtn6 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn5.mas_right);
    //        make.width.equalTo(myBtn5.mas_width);
    //        make.height.equalTo(myBtn5.mas_height);
    //        make.bottom.equalTo(myBtn5.mas_bottom);
    //        //            make.right.equalTo(myBtn3.mas_right).offset(-20);
    //    }];
    //
    //    [myBtn7 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(myBtn6.mas_right);
    //        make.width.equalTo(myBtn6.mas_width);
    //        make.height.equalTo(myBtn6.mas_height);
    //        make.bottom.equalTo(myBtn6.mas_bottom);
    //        make.right.equalTo(baseView.mas_right).offset(-0);
    //    }];
    //
    //
    //    [myBtn setImage:[UIImage imageNamed:@"stu_my_order"] forState:UIControlStateNormal];
    //    [myBtn1 setImage:[UIImage imageNamed:@"stu_my_task"] forState:UIControlStateNormal];
    //    [myBtn2 setImage:[UIImage imageNamed:@"stu_my_journal"] forState:UIControlStateNormal];
    //    [myBtn3 setImage:[UIImage imageNamed:@"stu_my_attendance"] forState:UIControlStateNormal];
    //    [myBtn4 setImage:[UIImage imageNamed:@"stu_my_leave"] forState:UIControlStateNormal];
    //    [myBtn5 setImage:[UIImage imageNamed:@"stu_my_visit"] forState:UIControlStateNormal];
    //    [myBtn6 setImage:[UIImage imageNamed:@"stu_my_achievement"] forState:UIControlStateNormal];
    //    [myBtn7 setImage:[UIImage imageNamed:@"submenu_classification_normal"] forState:UIControlStateNormal];
    //
    //    [myBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn1 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn2 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn3 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn4 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn5 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn6 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    //    [myBtn7 setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    UIButton *nameCellBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [StuSuperView addSubview:nameCellBtn];
    [nameCellBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(StuSuperView.mas_bottom);
        make.height.equalTo(@(123/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
    }];
    nameCellBtn.backgroundColor = [UIColor whiteColor];
    if(self.delegate){
        
        [nameCellBtn addTarget:self.delegate action:@selector(nameCellBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    _im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stu_see_portrait_normal"]];
    if (userIcon()) {
        _imagePath = userIcon();
        NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSLog(@"%@",paths);
        NSRange range = [_imagePath rangeOfString:@"Documents"];
        NSRange needRange = NSMakeRange(0, range.location+range.length);
        NSLog(@"%@",_imagePath);
        if (range.location != NSNotFound) {
            _imagePath = [_imagePath stringByReplacingCharactersInRange:needRange withString:paths];//每次更换沙盒路径需要重新替换新的路径，否则找不到文件。
        }
        
        NSLog(@"%@",_imagePath);
        
        UIImage *photoImg = [[UIImage alloc]initWithContentsOfFile:_imagePath];
        _im.image = photoImg;
        
    }

    
    
    [nameCellBtn addSubview:_im];
    [_im mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameCellBtn.mas_left).offset(30/2);
        make.centerY.equalTo(nameCellBtn.mas_centerY);
        make.width.equalTo(@(103/2));
        make.height.equalTo(@(103/2));
    }];
    _im.layer.masksToBounds = YES;
    _im.layer.cornerRadius = 12.0f;
    UILabel *nameLabel = [[UILabel alloc] init];
    [nameCellBtn addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameCellBtn.mas_top).offset(20/2);
        make.height.equalTo(@(30/2));
        make.left.equalTo(_im.mas_right).offset(30/2);
    }];
    [nameLabel sizeToFit];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *StuName = [ud objectForKey:@"username"];
    if (StuName) {
        nameLabel.text = StuName;
    }else{
        nameLabel.text = @"李小明";
    }
    
    nameLabel.textColor = [UIColor colorWithHex:0x333333];
    nameLabel.font = [UIFont systemFontOfSize:30/2];
    
    
    UILabel *KindStuLabel = [[UILabel alloc] init];
    [nameCellBtn addSubview:KindStuLabel];
    [KindStuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(16/2);
        make.left.equalTo(nameLabel.mas_left);
        make.height.equalTo(@(26/2));
    }];
    [KindStuLabel sizeToFit];
    
    KindStuLabel.text = @"正式学员";
    KindStuLabel.textColor = [UIColor colorWithHex:0xafafaf];
    UIView *bgView = [[UIView alloc] init];
    [StuSuperView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nameCellBtn.mas_top);
        make.height.equalTo(@(10/2));
        make.left.equalTo(StuSuperView.mas_left);
        make.right.equalTo(StuSuperView.mas_right);
        
    }];
    bgView.backgroundColor = [UIColor colorWithHex:0xf7f7f7];
    
    
    
    
    return StuSuperView;
}

- (void)changeImageView:(NSNotification *)sender{
    NSLog(@"%@",sender);
    
    NSLog(@"jslxyyxyyxyyxyyxyyxyyxyy");
    NSString *str = [sender object];
    if ([str isEqualToString:@"YES"]) {
        NSLog(@"grhslxyyxyyxyyxyyxyyxyyxyy");
        self.im.image = [self getLocalIcon];
    }else{
        self.im.image = [UIImage imageNamed:@"my_portrait_normal"];
    }
}

- (UIImage *)getLocalIcon{
        if (userIcon()) {
            _imagePath = userIcon();
        }else{
            return [UIImage imageNamed:@"my_portrait_normal"];
        }
    
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"%@",paths);
    NSRange range = [_imagePath rangeOfString:@"Documents"];
    NSRange needRange = NSMakeRange(0, range.location+range.length);
    NSLog(@"%@",_imagePath);
    if (range.location != NSNotFound) {
        _imagePath = [_imagePath stringByReplacingCharactersInRange:needRange withString:paths];//每次更换沙盒路径需要重新替换新的路径，否则找不到文件。
    }
    
    NSLog(@"%@",_imagePath);
    
    UIImage *photoImg = [[UIImage alloc]initWithContentsOfFile:_imagePath];
    return photoImg;
    
}


-(void)StuBtnClick:(StuButton *)btn{
    if (_delegate && [_delegate respondsToSelector:@selector(StuBtnClick:)]) {
        [_delegate StuBtnClick:btn];
    }
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
